package cn.tonyandmoney.voicenotes.ui.component

import android.content.Context
import android.view.Gravity
import android.view.View
import android.widget.TextView
import cn.tonyandmoney.voicenotes.notes.bean.ReaderState
import com.afollestad.materialdialogs.MaterialDialog
import org.jetbrains.anko.*

/**
 * 展示当前语音输入状态的界面
 */
class ReaderStateComponent(val operation: IDialogListener) : AnkoComponent<Context>, AnkoLogger {

    private lateinit var mContextView: TextView
    private lateinit var mTitleView: TextView
    private var mState = ReaderState()
    private val mView: View
    private var mDialog: MaterialDialog? = null

    @Volatile
    private var isShowing = false

    init {
        mView = createView(AnkoContext.Companion.create(operation as Context))
    }

    fun update(state: ReaderState) {
        mContextView.post {
            mContextView.text = ""
            mTitleView.text = state.operation.content
        }
        info("update: $state")
        mState = state
    }

    fun update(content: String) {
        mContextView.post { mContextView.append(content) }

    }


    fun show() {
        if (isShowing) return
        synchronized(isShowing) {
            isShowing = true
            mDialog = makeDialog(mView)
            mDialog!!.show()
        }
    }


    fun hide() {
        synchronized(isShowing) {
            mDialog?.dismiss()
            mDialog = null
            isShowing = false
        }
    }


    private fun makeDialog(view: View): MaterialDialog {
        return MaterialDialog.Builder(view.context)
                .title("语音识别")
                .customView(view, false)
                .positiveText("确定")
                .negativeText("取消")
                .autoDismiss(true)
                .cancelable(false)
                .dismissListener { isShowing = false }
                .onPositive { _, _ ->
                    operation.onConfirm(mState)
                }
                .onNegative { _, _ ->
                    operation.onCancel(mState)
                }
                .build()
    }

    override fun createView(ui: AnkoContext<Context>): View {
        return with(ui) {
            verticalLayout {
                backgroundResource = android.R.color.white
                leftPadding = dip(10)
                rightPadding = leftPadding
                textView {
                    padding = dip(10)
                    mTitleView = this
                    text = "输入名称"
                }.lparams(matchParent, wrapContent)
                scrollView {
                    textView {
                        padding = dip(10)
                        hint = "请输入内容"
                        mContextView = this
                        gravity = Gravity.START or Gravity.TOP
                    }.lparams(matchParent, wrapContent)
                }.lparams(matchParent, dip(120))
            }
        }
    }

    interface IDialogListener {
        fun onConfirm(state: ReaderState)
        fun onCancel(state: ReaderState)
    }
}